from django.db import models
import time

# Create your models here.
def get_proper_size(org_size):
    pro_size = org_size
    unit = 0
    while pro_size > 1024:
        unit += 1
        pro_size /= 1024
    pro_size = '%2.1f' % pro_size
    if unit == 0:
        return str(pro_size) + 'B'
    if unit == 1:
        return str(pro_size) + 'KB'
    if unit == 2:
        return str(pro_size) + 'MB'
    if unit == 3:
        return str(pro_size) + 'GB'
    if unit == 4:
        return str(pro_size) + 'TB'

class Entry:
    def __init__(self, entry_name, file_size, last_mod, is_dir):
        self.entry_name = entry_name
        self.file_size = get_proper_size(file_size)
        self.last_mod = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(last_mod))
        self.is_dir = is_dir
        
        if self.is_dir:
            self.gif = '/webdav/icons/folder.gif'
        elif entry_name.endswith('.mp4') or entry_name.endswith('.MP4') or entry_name.endswith('.wmv'):
            self.gif = '/webdav/icons/movie.gif'
        elif entry_name.endswith('.zip'):
            self.gif = '/webdav/icons/compressed.gif'